<?php
/**
 * 
 * UI Template Service
 * 
 * This service provides access to the various resources that make up a Digitalus template
 *
 * @package    	Digitalus\Ui
 * @copyright  	Copyright (c) 2009,  Digitalus Media USA (http://digitalusmedia.net)
 * @license    	New BSD License, library/Dig/License.txt
 * @version 	2.0
 * 
 */
class DigUi_Service_Template {
	/**
	 * these functions interact with the current ui 
	 */
    protected static $_templateParam = 'digitalus_template';
    protected static $_skinParam = 'digitalus_skin';
   const IS_AJAX_TEMPLATE = 'is_ajax_template';
   const IS_ADMIN_TEMPLATE = 'is_admin_template';
    
    public static function fromString($skinString)
    {
        $templateParts = explode('->', $skinString);
        if(is_array($templateParts)) {
            return array(
                'template'	=> $templateParts[0],
                'skin'		=> $templateParts[1]
            );
        }
    }
    
    public static function toString($template, $skin)
    {
        return $template . '->' . $skin;
    }
    
    public static function listTemplates()
    {
		$templateDirectory = DOCUMENT_ROOT . Dig_Service_Module::settings ( 'digUi', 'templatePath' );
		$templates = array();
        foreach (new DirectoryIterator($templateDirectory) as $fileInfo) {
            if($fileInfo->isDir() && !$fileInfo->isDot() && $fileInfo->getFilename() != '.svn') {
                $templates[] = $fileInfo->getFilename();
            }
        }
        return $templates;
    }
    
    public static function listSkins($template)
    {
        $templatePath = self::getPath($template, false);
        $skinPath = $templatePath . '/skins';
        $skins = array();
        foreach (new DirectoryIterator($skinPath) as $fileInfo) {
            if($fileInfo->isFile() && pathinfo($fileInfo->getFilename(), PATHINFO_EXTENSION) == 'ini') {
                $skins[] = pathinfo($fileInfo->getFilename(), PATHINFO_FILENAME);
            }
        }
        return $skins;
    }
    
    public static function useAdminTemplate()
    {
        $defaults = Dig_Service_Module::settings('digUi','default');
        $adminDefaults = $defaults['admin'];
        self::setTemplate($adminDefaults['template']);
        self::setSkin($adminDefaults['skin']);
        Dig_Service_Core::getInstance()->setParam(self::IS_ADMIN_TEMPLATE, true);
        // disable view block caching for administrators
        $view = Zend_Layout::getMvcInstance()->getView();
        $view->cacheEnabled = false;
    }

    public static function isAdminTemplate() {
        if(Dig_Service_Core::hasParam(self::IS_ADMIN_TEMPLATE)) {
            return Dig_Service_Core::getInstance()->getParam(self::IS_ADMIN_TEMPLATE);
        }
    }

    public static function useAjaxTemplate()
    {
        $defaults = Dig_Service_Module::settings('digUi','default');
        $ajaxDefaults = $defaults['ajax'];
        self::setTemplate($ajaxDefaults['template']);
        self::setSkin($ajaxDefaults['skin']);
        Dig_Service_Core::getInstance()->setParam(self::IS_AJAX_TEMPLATE, true);
        // disable view block caching for ajaxistrators
        Zend_Layout::getMvcInstance()->getView()->cacheEnabled = false;
    }

    public static function isAjaxTemplate() {
        if(Dig_Service_Core::hasParam(self::IS_AJAX_TEMPLATE)) {
            return Dig_Service_Core::getInstance()->getParam(self::IS_AJAX_TEMPLATE);
        }
    }
    
    public static function setTemplate($template)
    {
        Dig_Service_Firebug::profile('setting template to ' . $template);
        return Dig_Service_Core::setParam( self::$_templateParam, $template );
    }
    
    public static function setSkin($skin)
    {
        Dig_Service_Firebug::profile('setting skin to ' . $skin);
        return Dig_Service_Core::setParam( self::$_skinParam, $skin );
    }
      
	public static function currentTemplate() {
	    if(Dig_Service_Core::hasParam( self::$_templateParam )) {
	        return Dig_Service_Core::getParam( self::$_templateParam );
	    } else {
	        return self::defaultTemplate();
	    }
	}
	
	public static function defaultTemplate()
	{
        $defaults = Dig_Service_Module::settings('digUi','default');
        return $defaults ['template'];
	}

        public static function setItemSkin($item = null) {
            if(! self::isAdminTemplate() && self::layoutEnabled()) {
                $skin = $item->inheritContent('skin');
                if(!empty ($skin)) {
                    $skinParts = explode('->', $skin);
                    self::setTemplate($skinParts[0]);
                    self::setSkin($skinParts[1]);
                }
            }
        }
      
	public static function currentSkin() {
	    if(Dig_Service_Core::hasParam( self::$_skinParam )) {
	        $skin = Dig_Service_Core::getParam( self::$_skinParam );
	    } else {
	        $skin = self::defaultSkin();
	    }
            return self::getSkinConfig(self::currentTemplate(), $skin);
	}
	
	public static function defaultSkin()
	{
        $defaults = Dig_Service_Module::settings('digUi','default');
        return $defaults ['skin'];
	}
		
	public static function currentLayout() {
	    $skinConfig = self::currentSkin();
	    return $skinConfig->layout;
	}
	
    public static function getBlocksBySkin($template, $skin)
    {
        $config = self::getSkinConfig($template, $skin);
        if($config instanceof Zend_Config ) {
            return $config->blocks->toArray();
        }
    }
	
	public static function getSkinConfig($template, $skin)
	{
	    $templatePath = self::getPath($template, false);
            Dig_Service_Firebug::profile('loading skin config from ' . $templatePath);
	    return new Zend_Config_Ini($templatePath . '/skins/' . $skin . '.ini');
	}
	
	/**
	 * returns the path to the specied template
	 * if relative is set to true this path is relative to the document root
	 * otherwise it is root relative
	 *
	 * @param string $template
	 * @param boolean $relative
	 * @return string
	 */
	public static function getPath($template = null, $relative = true) {
	    if($template == null) {
	        $template = self::currentTemplate();
	    }
            if ($relative == false) {
                    $base = DOCUMENT_ROOT;
            } else {
                    $base = null;
            }
            return $base . Dig_Service_Module::settings ( 'digUi', 'templatePath' ) . '/' . $template;
	}
	
	public static function addStylesheet($stylesheet) {
		if (substr ( $stylesheet, 0, 1 ) == '/' || substr ( $stylesheet, 0, 4 ) == 'http') {
			$filepath = $stylesheet;
		} else {
			$filepath = self::getPath() . '/css/' . $stylesheet;
		}
                Dig_Service_Firebug::profile('adding stylesheet - ' . $filepath);
                $view = Zend_Layout::getMvcInstance()->getView();
                $view->headLink()->appendStylesheet($filepath);
	}

        public static function addCss($css) {
             Zend_Layout::getMvcInstance()->getView()->headStyle()->appendStyle($css);
        }

	public static function addJavascript($jsFile) {
		if (substr ( $jsFile, 0, 1 ) == '/' || substr ( $jsFile, 0, 4 ) == 'http') {
			$filepath = $jsFile;
		} else {
			$filepath = self::getPath() . '/javascript/' . $jsFile;
		}
            $view = Zend_Layout::getMvcInstance()->getView();
            $view->jQuery()->addJavascriptFile($filepath);
	}

        public static function layoutEnabled() {
            return Zend_Layout::getMvcInstance()->isEnabled();
        }
}
?>